import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)

/* Layout */
import Layout from '@/layout'

var constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  // 仪表盘
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '主页', icon: 'dashboard' }
    }]
  },
  // 权限管理
  {
    path: '/authority',
    component: Layout,
    redirect: '/authority/account',
    name: 'Authority',
    meta: { title: '权限管理', icon: 'el-icon-s-custom' },
    children: [
      {
        path: 'account',
        name: 'Account',
        component: () => import('@/views/authority/account/index'),
        meta: { title: '账号管理', icon: 'el-icon-user-solid' }
      },
      {
        path: 'role',
        name: 'Role',
        component: () => import('@/views/authority/role/index'),
        meta: { title: '角色管理', icon: 'el-icon-user' }
      },
      {
        path: 'resource',
        name: 'Resource',
        component: () => import('@/views/authority/myResouce/index'),
        meta: { title: '资源管理', icon: 'el-icon-folder' }
      }
    ]
  },
  // 商品管理
  {
    path: '/commodities',
    component: Layout,
    redirect: '/commodities/list',
    name: 'Example',
    meta: { title: '商品管理', icon: 'el-icon-s-shop' },
    children: [
      {
        path: 'list',
        name: 'List',
        component: () => import('@/views/commodities/commoditiesList/index'),
        meta: { title: '商品列表', icon: 'el-icon-shopping-bag-1' }
      },
      {
        path: 'addCommodity',
        name: 'AddCommodity',
        component: () => import('@/views/commodities/commoditiesList/comDetail/index'),
        hidden: true,
        meta: {
          title: '新增商品',
          activeMenu: '/commodities/commoditiesList',
          // 让路由同一个页面有不同的路由地址
          componentUrl: '@/views/commodities/commoditiesList/comDetail/index'
        }
      },
      {
        path: 'editCommodity/:id',
        name: 'EditCommodity',
        component: () => import('@/views/commodities/commoditiesList/comDetail/index'),
        hidden: true,
        meta: {
          title: '修改商品',
          activeMenu: '/commodities/commoditiesList',
          componentUrl: '@/views/commodities/commoditiesList/comDetail/index'
        }
      },
      {
        path: 'category',
        name: 'Category',
        component: () => import('@/views/commodities/category/index'),
        meta: { title: '商品分类', icon: 'el-icon-menu' }
      },
      {
        path: 'manage',
        name: 'Manage',
        component: () => import('@/views/commodities/brandList/index'),
        meta: { title: '品牌管理', icon: 'el-icon-money' }
      }
    ]
  },
  // 订单管理
  {
    path: '/orders',
    component: Layout,
    redirect: '/orders/list',
    name: 'Orders',
    meta: { title: '订单管理', icon: 'el-icon-s-order' },
    children: [
      {
        path: 'orderList',
        name: 'OrderList',
        component: () => import('@/views/orders/orderList/index'),
        meta: { title: '订单列表', icon: 'el-icon-document-checked' }
      },
      {
        path: 'orderDetail/:id',
        name: 'OrderDetail',
        component: () => import('@/views/orders/orderList/oderDetail/index'),
        hidden: true,
        meta: {
          title: '订单详情',
          // 页面替换时激活组件列表
          activeMenu: '/orders/list'
        }
      },
      {
        path: 'returnList',
        name: 'ReturnList',
        component: () => import('@/views/orders/returnList/index'),
        meta: { title: '退单列表', icon: 'el-icon-document-delete' }
      },
      {
        path: 'returnDetail/:id',
        name: 'ReturnDetail',
        component: () => import('@/views/orders/returnList/returnDetail/index'),
        hidden: true,
        meta: {
          title: '退单详情',
          // 页面替换时激活组件列表
          activeMenu: '/orders/list'
        }
      },
      {
        path: 'addressManage',
        name: 'AddressManage',
        component: () => import('@/views/orders/addressManage/index'),
        meta: { title: '地址管理', icon: 'el-icon-map-location' }
      }
    ]
  },
  // 注册用户管理
  {
    path: '/users',
    component: Layout,
    alwaysShow: true,
    redirect: '/users',
    name: 'Users',
    meta: { title: '注册用户管理', icon: 'el-icon-s-custom' },
    children: [
      {
        path: 'userList',
        name: 'UserList',
        component: () => import('@/views/users/userList/index'),
        meta: { title: '用户列表', icon: 'el-icon-user' }
      }
    ]
  },
  // 营销管理
  {
    path: '/market',
    component: Layout,
    redirect: '/market',
    name: 'Market',
    meta: { title: '营销管理', icon: 'el-icon-shopping-bag-1' },
    children: [
      {
        path: 'activity',
        name: 'Activity',
        component: () => import('@/views/market/activity/index'),
        meta: { title: '限时活动', icon: 'el-icon-s-ticket' }
      },
      {
        path: 'advertise',
        name: 'Advertise',
        component: () => import('@/views/market/advertise/index'),
        meta: { title: '广告列表', icon: 'el-icon-document-copy' }
      }
    ]
  },
  // 内容管理
  {
    path: '/contents',
    component: Layout,
    redirect: '/contents',
    name: 'Contents',
    meta: { title: '内容管理', icon: 'el-icon-folder' },
    children: [
      {
        path: 'article',
        name: 'Article',
        component: () => import('@/views/contents/article/index'),
        meta: { title: '文章列表', icon: 'el-icon-tickets' }
      },
      {
        path: 'material',
        name: 'Material',
        component: () => import('@/views/contents/material/index'),
        meta: { title: '素材列表', icon: 'el-icon-picture-outline' }
      },
      {
        path: 'addArticle',
        name: 'AddArticle',
        component: () => import('@/views/contents/article/detail/index'),
        hidden: true,
        meta: {
          title: '新增文章',
          activeMenu: '/content/article',
          // 让路由同一个页面有不同的路由地址
          componentUrl: '@/views/content/article/detail/index'
        }
      },
      {
        path: 'editArticle/:id',
        name: 'EditArticle',
        component: () => import('@/views/contents/article/detail/index'),
        hidden: true,
        meta: {
          title: '修改文章',
          activeMenu: '/content/article',
          componentUrl: '@/views/content/article/detail/index'
        }
      }
    ]
  },
  // 个人中心
  {
    path: '/personal',
    component: Layout,
    redirect: '/personal',
    name: 'Personal',
    meta: { title: '个人中心', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('@/views/personal/home/index'),
        meta: { title: '个人主页', icon: 'home' }
      },
      {
        path: 'pSetting',
        name: 'PSetting',
        component: () => import('@/views/personal/pSetting/index'),
        meta: { title: '个人设置', icon: 'pSetting' }
      }
    ]
  },
  // 设置
  {
    path: '/setting',
    component: Layout,
    children: [{
      path: 'setting',
      name: 'Setting',
      component: () => import('@/views/setting/index'),
      meta: { title: '设置', icon: 'dashboard' }
    }]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]
// getInitMenus().then(res => {
//   res.data.permissionList.forEach(el => {
//     el.meta = JSON.parse(el.meta)
//     el.children.forEach(ele => {
//       ele.meta = JSON.parse(ele.meta)
//     })
//   })
//   constantRoutes = [...constantRoutes, ...res.data.permissionList]
//   console.log(constantRoutes)
// })
const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// 重置路由
export function resetRouter() {
  const newRouter = createRouter()
  console.log(router)
  router.matcher = newRouter.matcher // reset router
}

export default router
